<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="地址编号" prop="terminalNo">
        <el-input v-model="queryParams.terminalNo" placeholder="请输入地址编号" clearable style="width: 200px" />
      </el-form-item>
      <el-form-item label="用户编号" prop="phoneNo">
        <el-input v-model="queryParams.phoneNo" placeholder="请输入用户编号" clearable style="width: 200px" />
      </el-form-item>
      <el-form-item label="品牌名称" prop="smName">
        <el-input v-model="queryParams.smName" placeholder="请输入品牌名称" clearable style="width: 200px" />
      </el-form-item>
      <el-form-item label="状态名称" prop="runName">
        <el-select v-model="queryParams.runName" placeholder="请选择状态名称" clearable style="width: 200px">
          <el-option 
            v-for="dict in dict.type.sys_normal_disable" 
            :key="dict.value" 
            :label="dict.label" 
            :value="dict.value" 
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户等级" prop="ownerName">
        <el-select v-model="queryParams.ownerName" placeholder="请选择用户等级" clearable style="width: 200px">
          <el-option 
            v-for="dict in dict.type.guangdian_user_level" 
            :key="dict.value" 
            :label="dict.label" 
            :value="dict.value" 
          />
        </el-select>
      </el-form-item>
      <el-form-item label="重度用户" prop="isHeavyUser">
        <el-select v-model="queryParams.isHeavyUser" placeholder="请选择是否重度用户" clearable style="width: 200px">
          <el-option 
            v-for="dict in dict.type.guangdian_heavy_user" 
            :key="dict.value" 
            :label="dict.label" 
            :value="dict.value" 
          />
        </el-select>
      </el-form-item>
      <el-form-item label="电视消费" prop="tvConsumptionLevel">
        <el-select v-model="queryParams.tvConsumptionLevel" placeholder="请选择电视消费水平" clearable style="width: 200px">
          <el-option 
            v-for="dict in dict.type.guangdian_consumption_level" 
            :key="dict.value" 
            :label="dict.label" 
            :value="dict.value" 
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="el-icon-document-add" size="mini" @click="handleAdd">新增</el-button>
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="guangDianUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="地址编号" align="center" prop="terminalNo" width="180" />
      <el-table-column label="用户编号" align="center" prop="phoneNo" width="180" />
      <el-table-column label="品牌名称" align="center" prop="smName" width="120" />
      <el-table-column label="状态名称" align="center" prop="runName" width="100">
        <template slot-scope="scope">
          <el-tag :type="statusType[scope.row.runName] || 'info'">
            {{ scope.row.runName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户等级" align="center" prop="ownerName" width="100" />
      <el-table-column label="重度用户" align="center" prop="isHeavyUser" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isHeavyUser === '是' ? 'warning' : 'info'">
            {{ scope.row.isHeavyUser }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="电视消费" align="center" prop="tvConsumptionLevel" width="100">
        <template slot-scope="scope">
          <el-tag :type="consumptionLevelType[scope.row.tvConsumptionLevel] || 'info'">
            {{ scope.row.tvConsumptionLevel }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="宽带消费" align="center" prop="broadbandConsumptionLevel" width="100" />
      <el-table-column label="忠诚用户" align="center" prop="isLoyalUser" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isLoyalUser === '是' ? 'success' : 'info'">
            {{ scope.row.isLoyalUser }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="服务品牌" align="center" prop="serviceBrand" width="100" />
      <el-table-column label="TV渗透率" align="center" prop="tvPenetration" width="100" />
      <el-table-column label="宽带渗透" align="center" prop="broadbandPenetration" width="100" />
      <el-table-column label="用户类型" align="center" prop="userType" width="100" />
      <el-table-column label="状态更新" align="center" prop="runTime" width="180" />
      <el-table-column label="开户时间" align="center" prop="openTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:guangdian:view']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            v-hasPermi="['system:guangdian:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:guangdian:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改广电用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="地址编号" prop="terminalNo">
              <el-input v-model="form.terminalNo" placeholder="请输入地址编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户编号" prop="phoneNo">
              <el-input v-model="form.phoneNo" placeholder="请输入用户编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品牌名称" prop="smName">
              <el-input v-model="form.smName" placeholder="请输入品牌名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态名称" prop="runName">
              <el-select v-model="form.runName" placeholder="请选择状态名称">
                <el-option 
                  v-for="dict in dict.type.sys_normal_disable" 
                  :key="dict.value" 
                  :label="dict.label" 
                  :value="dict.value" 
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品牌编号" prop="smCode">
              <el-input v-model="form.smCode" placeholder="请输入品牌编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户等级" prop="ownerName">
              <el-select v-model="form.ownerName" placeholder="请选择用户等级">
                <el-option 
                  v-for="dict in dict.type.guangdian_user_level" 
                  :key="dict.value" 
                  :label="dict.label" 
                  :value="dict.value" 
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="等级编号" prop="ownerCode">
              <el-input v-model="form.ownerCode" placeholder="请输入等级编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态更新时间" prop="runTime">
              <el-date-picker
                v-model="form.runTime"
                type="datetime"
                placeholder="请选择状态更新时间"
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="完整地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入完整地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="街道小区" prop="estateName">
              <el-input v-model="form.estateName" placeholder="请输入街道小区" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开户时间" prop="openTime">
              <el-date-picker
                v-model="form.openTime"
                type="datetime"
                placeholder="请选择开户时间"
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宽带是否生效" prop="force">
              <el-select v-model="form.force" placeholder="请选择宽带是否生效">
                <el-option value="" label="请选择" />
                <el-option value="是" label="是" />
                <el-option value="否" label="否" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="重度用户" prop="isHeavyUser">
              <el-select v-model="form.isHeavyUser" placeholder="请选择是否重度用户">
                <el-option 
                  v-for="dict in dict.type.guangdian_heavy_user" 
                  :key="dict.value" 
                  :label="dict.label" 
                  :value="dict.value" 
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电视消费" prop="tvConsumptionLevel">
              <el-select v-model="form.tvConsumptionLevel" placeholder="请选择电视消费水平">
                <el-option 
                  v-for="dict in dict.type.guangdian_consumption_level" 
                  :key="dict.value" 
                  :label="dict.label" 
                  :value="dict.value" 
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宽带消费" prop="broadbandConsumptionLevel">
              <el-select v-model="form.broadbandConsumptionLevel" placeholder="请选择宽带消费水平">
                <el-option 
                  v-for="dict in dict.type.guangdian_consumption_level" 
                  :key="dict.value" 
                  :label="dict.label" 
                  :value="dict.value" 
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="忠诚用户" prop="isLoyalUser">
              <el-select v-model="form.isLoyalUser" placeholder="请选择是否忠诚用户">
                <el-option 
                  v-for="dict in dict.type.guangdian_loyal_user" 
                  :key="dict.value" 
                  :label="dict.label" 
                  :value="dict.value" 
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务品牌" prop="serviceBrand">
              <el-select v-model="form.serviceBrand" placeholder="请选择服务品牌">
                <el-option 
                  v-for="dict in dict.type.guangdian_service_brand" 
                  :key="dict.value" 
                  :label="dict.label" 
                  :value="dict.value" 
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="TV渗透率" prop="tvPenetration">
              <el-select v-model="form.tvPenetration" placeholder="请选择TV渗透率">
                <el-option 
                  v-for="dict in dict.type.guangdian_penetration" 
                  :key="dict.value" 
                  :label="dict.label" 
                  :value="dict.value" 
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宽带渗透" prop="broadbandPenetration">
              <el-select v-model="form.broadbandPenetration" placeholder="请选择宽带渗透率">
                <el-option 
                  v-for="dict in dict.type.guangdian_penetration" 
                  :key="dict.value" 
                  :label="dict.label" 
                  :value="dict.value" 
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户类型" prop="userType">
              <el-select v-model="form.userType" placeholder="请选择用户类型">
                <el-option 
                  v-for="dict in dict.type.guangdian_user_type" 
                  :key="dict.value" 
                  :label="dict.label" 
                  :value="dict.value" 
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看广电用户详情对话框 -->
    <el-dialog :title="'广电用户详情'" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-form :model="form" label-width="100px" size="small">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="地址编号">
              <el-tag>{{ form.terminalNo }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户编号">
              <el-tag>{{ form.phoneNo }}</el-tag>
            </el-form-item>
          </el-col>
          <!-- 其他详情字段类似展示 -->
          <el-col :span="12">
            <el-form-item label="品牌名称">
              <el-tag>{{ form.smName }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态名称">
              <el-tag :type="statusType[form.runName] || 'info'">
                {{ form.runName }}
              </el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户等级">
              <el-tag>{{ form.ownerName }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="重度用户">
              <el-tag :type="form.isHeavyUser === '是' ? 'warning' : 'info'">
                {{ form.isHeavyUser }}
              </el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电视消费">
              <el-tag :type="consumptionLevelType[form.tvConsumptionLevel] || 'info'">
                {{ form.tvConsumptionLevel }}
              </el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="忠诚用户">
              <el-tag :type="form.isLoyalUser === '是' ? 'success' : 'info'">
                {{ form.isLoyalUser }}
              </el-tag>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGuangDianUser, getGuangDianUser, delGuangDianUser, addGuangDianUser, updateGuangDianUser } from "@/api/system/guangdian";

export default {
  name: "GuangDianUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广电用户表格数据
      guangDianUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      detailOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        terminalNo: undefined,
        phoneNo: undefined,
        smName: undefined,
        runName: undefined,
        ownerName: undefined,
        isHeavyUser: undefined,
        tvConsumptionLevel: undefined,
        broadbandConsumptionLevel: undefined,
        isLoyalUser: undefined,
        serviceBrand: undefined,
        tvPenetration: undefined,
        broadbandPenetration: undefined,
        userType: undefined,
        beginRunTime: undefined,
        endRunTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        terminalNo: [
          { required: true, message: "地址编号不能为空", trigger: "blur" }
        ],
        phoneNo: [
          { required: true, message: "用户编号不能为空", trigger: "blur" }
        ],
        runName: [
          { required: true, message: "状态名称不能为空", trigger: "change" }
        ]
      },
      // 状态对应的标签类型
      statusType: {
        "正常": "success",
        "停用": "danger"
      },
      // 消费水平对应的标签类型
      consumptionLevelType: {
        "高": "danger",
        "中": "warning",
        "低": "info"
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询广电用户列表 */
    getList() {
      this.loading = true;
      listGuangDianUser(this.queryParams).then(response => {
        this.guangDianUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增广电用户";
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.reset();
      const id = row.id || this.ids
      getGuangDianUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改广电用户";
      });
    },
    /** 查看按钮操作 */
    handleDetail(row) {
      const id = row.id || this.ids
      getGuangDianUser(id).then(response => {
        this.form = response.data;
        this.detailOpen = true;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        `是否确认删除广电用户数据?`,
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delGuangDianUser(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.download("system/guangdian/export", queryParams, `guangdian_${new Date().getTime()}.xlsx`);
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        terminalNo: undefined,
        phoneNo: undefined,
        smName: undefined,
        runName: undefined,
        smCode: undefined,
        ownerName: undefined,
        ownerCode: undefined,
        runTime: undefined,
        address: undefined,
        estateName: undefined,
        openTime: undefined,
        force: undefined,
        isHeavyUser: undefined,
        tvConsumptionLevel: undefined,
        broadbandConsumptionLevel: undefined,
        isLoyalUser: undefined,
        serviceBrand: undefined,
        tvPenetration: undefined,
        broadbandPenetration: undefined,
        userType: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateGuangDianUser(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addGuangDianUser(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.detailOpen = false;
      this.reset();
    }
  }
};
</script>